package yoyo.secret.app;

import yoyo.secret.app.AddAccountFragment.OnNotify;
import yoyo.secret.app.base.YoyoApplication;
import yoyo.secret.app.db.DataBaseOperation;
import yoyo.secret.app.widget.ToggleImageButton;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ContentActivity extends FragmentActivity {

	private TextView textView;
	private FragmentTransaction ft;
	private FragmentManager manager;
	private AddAccountFragment addFragment;
	private AccountInfoListFragment listFragment;
	private boolean ishide;

	private DataBaseOperation mDataBaseOperation;

	private String classification;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.account_content_layout);
		mDataBaseOperation = ((YoyoApplication) getApplication()).dataBaseOperation;

		final ToggleImageButton addButton = (ToggleImageButton) findViewById(R.id.add);
		textView = (TextView) findViewById(R.id.hint_tips);
		textView.setVisibility(View.INVISIBLE);
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textView.setVisibility(View.GONE);
				addAccountFragment(addButton.getButtonState());
			}
		});

		Intent intent = getIntent();
		if (intent != null) {
			classification = intent.getStringExtra("class");
			Log.i("", "添加类型:" + classification);
		}
		init();
		showList();
	}

	private void init() {
		manager = getSupportFragmentManager();
		addFragment = AddAccountFragment.newInstance(mDataBaseOperation, classification);
		listFragment = AccountInfoListFragment.newInstance(mDataBaseOperation, classification);
	}

	private void showList() {
		ft = manager.beginTransaction();
		ft.addToBackStack(null);
		ft.add(R.id.content_add, listFragment);
		ft.commit();
	}

	private void addAccountFragment(boolean params) {
		ft = manager.beginTransaction();
		ft.addToBackStack(null);
		ft.add(R.id.content_add, addFragment);
		if (params) {
			ft.show(addFragment);
			ishide = true;
		} else {
			ishide = false;
			ft.hide(addFragment);
		}

		ft.commit();

		addFragment.setOnNotify(new OnNotify() {

			@Override
			public void onNotify() {
				Log.i("", "---------------------onNotify---------------------");
				listFragment.getAdapter().notifyDataSetChanged();
				ft = manager.beginTransaction();
				ft.addToBackStack(null);
				ishide = !ishide;
				ft.hide(addFragment);
				ft.commit();
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Log.i("", "-------------------------1-----------------" + ishide);
		if (keyCode == KeyEvent.KEYCODE_BACK && ishide) {
			Log.i("", "---------------------2---------------------" + ishide);
			addAccountFragment(false);
			return true;
		}
		this.setResult(1000);
		ContentActivity.this.finish();
		return super.onKeyDown(keyCode, event);
	}
}
